package com.hundun.vision.biz.core;

import org.ta4j.core.Rule;
import org.ta4j.core.TradingRecord;
import org.ta4j.core.rules.AndRule;
import org.ta4j.core.rules.OrRule;

/**
 * @author ：RuoChen
 * @date ：15:17 2024/12/6
 * @description：合约规则
 */
public class ContractRule implements Rule {
    /**
     * 规则
     */
    private final Rule rule;
    /**
     * 是否多单规则：命中规则后需要创建的订单类型
     */
    private final boolean isBuy;

    public ContractRule(Rule rule, boolean isBuy) {
        this.rule = rule;
        this.isBuy = isBuy;
    }

    public boolean isBuy() {
        return isBuy;
    }

    @Override
    public boolean isSatisfied(int index, TradingRecord tradingRecord) {
        return rule.isSatisfied(index, tradingRecord);
    }

    @Override
    public Rule or(Rule rule) {
        ContractRule contractRule = (ContractRule) rule;
        boolean isBuy = this.isBuy && contractRule.isBuy;
        OrRule orRule = new OrRule(this, rule);
        return new ContractRule(orRule, isBuy);
    }

    @Override
    public Rule and(Rule rule) {
        ContractRule contractRule = (ContractRule) rule;
        boolean isBuy = this.isBuy && contractRule.isBuy;
        AndRule andRule = new AndRule(this, rule);
        return new ContractRule(andRule, isBuy);
    }
}